﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace StudentManagement
{
    public partial class frmGuanli : Form
    {
        public frmGuanli()
        {
            InitializeComponent();
        }

        private void tianjia_Click(object sender, EventArgs e)
        {
            frmTianjia fr = new frmTianjia();
            DialogResult dr = fr.ShowDialog();
            if (dr == DialogResult.OK)
            {
                BindingData();
            }
        }
        public void BindingData()
        {
            string connstr = "server=.;database=Class;user=sa;password=123456";
            SqlConnection conn = new SqlConnection(connstr);
            conn.Open();
            string selectsql = "SELECT Id,Name,sex,birthday,policy,nation,idcard FROM student";
            SqlCommand comm = new SqlCommand(selectsql, conn);
            SqlDataReader dr = comm.ExecuteReader();
            List<Student> stuList = new List<Student>();
            while (dr.Read())
            {
                Student s = new Student();
                s.Id = dr.GetString(0);
                s.Name = dr.GetString(1);
                s.Sex = dr.GetString(2);
                s.Birthday = dr.GetString(3);
                s.Policy = dr.GetString(4);
                s.Nation = dr.GetString(5);
                s.IdCard = dr.GetString(6);

                stuList.Add(s);
            }
            conn.Close();
            this.dgvguanli.DataSource = stuList;
        }

        private void delete_Click(object sender, EventArgs e)
        {
            string id = this.dgvguanli.SelectedRows[0].Cells[0].Value.ToString();
            string connstr = "server=.;database=Class;user=sa;password=123456";
            SqlConnection conn = new SqlConnection(connstr);
            conn.Open();
            string sql = "delete from student where id='"+id+"'";
            SqlCommand comm = new SqlCommand(sql, conn);
            int count = comm.ExecuteNonQuery();
            if (count > 0)
            {
                BindingData();
            }
        }

        private void frmGuanli_Load(object sender, EventArgs e)
        {
            BindingData();
        }

  

  

  


    }
}
